var webRoot = 'http://app252.51edn.com';
var apiRoot = webRoot + '/mobile/index.php';
var pic = webRoot + "/data/upload/shop/store/slide/";	
var pica = webRoot + "/data/upload/shop/store/"; //手机
var pics = webRoot + "/data/upload/shop/store/goods/";//商品图片
var userpic = webRoot + "/data/upload/shop/avatar/";//用户头像
var user_pic = webRoot + "/data/upload/shop/avatar/";//用户头像
var cam_pic = webRoot + "/data/upload/shop/member/5/";
var app_pic = webRoot + "/data/upload/shop/member/";
var circle_pic = webRoot + "/data/upload/circle/theme/";
var circle = webRoot + "/circle";


//var p1=/^(13[0-9]\d{8}|15[0-35-9]\d{8}|18[0-9]\d{8}|14[57]\d{8})$/;//手机号码格式验证
var p1=/^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;//手机号码格式验证
var reg = /^[0-9a-zA-Z]*$/g;//只能输入数字字母
var isIDCard1 =/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/; 
var numb = /^(0|[1-9][0-9]*)$/;			//验证数字
var ages = /^(?:[1-9]?\d|130)$/;         //验证年龄
var codes = '';

/////////////
/**
 * 获取验证码
 * @param user 手机号码  注册
 * @return null
 */
function getverifyr(user){
//	console.log(user);
	$.ajax({
		type : 'post' , 
		url :apiRoot + '?act=login&op=code_r',
		data:{tel : user},
		dataType:'json',
		success : function(data){
			console.log(JSON.stringify(data));
			if(data.code == 400){
				toast(data.datas.error);
			}else{
				var info = $.parseJSON(data.yz);
				plus.nativeUI.toast(info.msg);
				codes = data.yzm;
				time_asc()
			}
		} , 
		error : function(e){
			console.log(JSON.stringify(e));
			plus.nativeUI.toast('失去网络连接');
		}
	})
}
/**
 * 获取验证码
 * @param user 手机号码
 * @return null
 */
function getverify(user){
//	console.log(user);
	$.ajax({
		type : 'post' , 
		url :apiRoot + '?act=login&op=code',
		data:{tel : user},
		dataType:'json',
		success : function(data){
			console.log(JSON.stringify(data));
			if(data){
				var info = $.parseJSON(data.yz);
				plus.nativeUI.toast(info.msg);
				codes = data.yzm;
				time_asc()
			}
		} , 
		error : function(e){
			console.log(JSON.stringify(e));
			plus.nativeUI.toast('失去网络连接');
		}
	})
}
/**
 * 倒计时
 */
function time_asc(){
//	console.log(122);
	$('#daojishi').attr('disabled' , true);
	var code = '';
	var second = 60;
	code = setInterval(function(){
		if(second <= 0){
			clearInterval(code);
			reset_input(code);  
		}else{
			$('#daojishi').text(second -- + 's');
		}
	} , 1000);  
}
/**
 * 重设状态
 */
function reset_input(code){
	clearInterval(code);
	$('#daojishi').removeAttr('disabled');
	$('#daojishi').text('重发'); 
}

/**
 * 输出
 */
function toast(info){
	plus.nativeUI.toast(info);
}


/**
 * 错误输出
 */
function error(){
	plus.nativeUI.toast('连接失败！请检查网络连接！');
}


/**
 * 取消订单
 */
function order_cancel(member_id,order_id,spage){
	$.ajax({
		type:"post",
		url:apiRoot + "?act=member_order&op=order_cancel",
		data:{
			member_id:member_id,
			order_id:order_id
		},
		dataType:'json',
		success:function(data){
//			console.log(JSON.stringify(data));
			if(data.code == 200){
				toast('取消订单成功！');
				if(spage){
					plus.webview.getWebviewById(spage).reload();
					ws.reload();
				}else{
					ws.reload();
				}
			}else{
				toast('取消订单失败！');
			}
		},
		error:function(e){
//			console.log(JSON.stringify(e));
			error();
		}
	});
}

/**
 * 确认收货
 */
function order_receive(member_id,order_id,spage){
//	console.log(apiRoot + "?act=member_order&op=order_receive"+member_id+order_id);return;
	$.ajax({
		type:"post",
		url:apiRoot + "?act=member_order&op=order_receive",
		data:{
			member_id:member_id,
			order_id:order_id
		},
		dataType:'json',
		success:function(data){
			console.log(JSON.stringify(data));
			if(data.code == 200){
				toast('收货成功！');
				if(spage){
					plus.webview.getWebviewById(spage).reload();
					ws.reload();
				}else{
					ws.reload();
				}
			}else{
				toast(data.datas.error);
			}
		},
		error:function(e){
//			console.log(JSON.stringify(e));
			error();
		}
	});
}

/**
 * 确认收货
 */
function order_complete(member_id,order_id,spage){
//	console.log(apiRoot + "?act=member_order&op=order_complete"+member_id+order_id);return;
	$.ajax({
		type:"post",
		url:apiRoot + "?act=member_order&op=order_complete",
		data:{
			member_id:member_id,
			order_id:order_id
		},
		dataType:'json',
		success:function(data){
			console.log(JSON.stringify(data));
			if(data.code == 200){
				toast('操作成功！');
				if(spage){
					plus.webview.getWebviewById(spage).reload();
					ws.reload();
				}else{
					ws.reload();
				}
			}else{
				toast(data.datas.error);
			}
		},
		error:function(e){
//			console.log(JSON.stringify(e));
			error();
		}
	});
}



/**
 * 显示图片
 * @param {String} pic 图片路径
 */
var getuserPic = function(pic) {
	if(pic == null || pic == '') {
		return '';
	}
	if(pic.indexOf('http') < 0 && pic.indexOf('../images') >= 0) {
		return pic;
	}
	return pic.indexOf('http') < 0 ? webRoot + pic : pic;
}

//赞
function likeYes(o, options) {
	$.getJSON(circle + '/index.php?act=theme&op=ajax_likeyes&c_id=' + options.c_id + '&t_id=' + options.t_id + '&u_id=' + options.u_id, function(data) {
		if(data) {
			var likeCount = parseInt(o.find('.likecount').html()) + 1;
			o.find('.likecount').html(likeCount);
			o.find('.iconfont').html('&#xe616;');
			o.unbind().click(function() {
				likeNo(o, options);
			})
		}
	});
}

//取消赞
function likeNo(o, options) {
	$.getJSON(circle + '/index.php?act=theme&op=ajax_likeno&c_id=' + options.c_id + '&t_id=' + options.t_id + '&u_id=' + options.u_id, function(data) {
		if(data) {
			var likeCount = parseInt(o.find('.likecount').html()) - 1;
			o.find('.likecount').html(likeCount);
			o.find('.iconfont').html('&#xe614;');
			o.unbind().click(function() {
				likeYes(o, options);
			})
		}
	});
}

//发布评论	r_id是回复楼层数
function gotoreply(c_id, t_id,r_id) {
	var uid = plus.storage.getItem('id');
	if(!uid) {
		openNewPage("login.html");
	} else {
		//这里流星处理一下白屏问题
		var nwaiting = plus.nativeUI.showWaiting();//显示原生等待框
		var webviewContent= plus.webview.create("Friends_evaluation.html", "Friends_evaluation.html", '', {
			t_id: t_id,
			c_id: c_id,
			r_id:r_id,
			spage:ws.id
		});
		webviewContent.addEventListener("loaded", function() { //注册新webview的载入完成事件
	        nwaiting.close(); //新webview的载入完毕后关闭等待框
	        webviewContent.show("slide-in-right",500); //把新webview窗体显示出来，显示动画效果为速度200毫秒的右侧移入动画
		}, false);
 
	}
}


function relogin(_self) {
	var all = plus.webview.all();
	for(var i in all) {
		if(all[i].id !== plus.runtime.appid && all[i].id !== _self) {
			all[i].close();
		}
		if(i == all.length - 1) {
			var nwaiting = plus.nativeUI.showWaiting(); //显示原生等待框
			var webviewContent = plus.webview.create('../index.html', 'index.html'); //后台创建webview并打开show.html
			webviewContent.addEventListener("loaded", function() { //注册新webview的载入完成事件
				setTimeout(function(){
			        nwaiting.close(); //新webview的载入完毕后关闭等待框
		        },2000)
				webviewContent.show("slide-in-right", 500); //把新webview窗体显示出来，显示动画效果为速度200毫秒的右侧移入动画
				ws.close();
			}, false);
		}
	}
}

/*
 拨打电话
 
 * */ 
$('.tel').on('click',function(){
		var tel = $(this).attr('data-tel');
//		console.log(tel);
		plus.device.dial( tel, false );
})
$(".user").on('click',function(){
    $(".blus").css("color","#338efc");  
})
$(".phon").on('click',function(){
    $(".blus1").css("color","#338efc");  
})
$(".passw").on('click',function(){
	$(".blus2").css("color","#338efc");  
})
